﻿namespace H2.Streams.Sections
{
    using H2.Streams;
    using H2.Streams.RawTypes;
    using System;
    using System.Collections.Generic;
    using System.Reflection;

    public class BitmapCollection
    {
        private Bitmap[] BitmapCache;
        private MapStream Map;

        internal BitmapCollection(MapStream map, int offset)
        {
            int num = map.ReadReflexiveAt(offset + 0x44);
            this.BitmapCache = new Bitmap[num];
            for (int i = 0; i < num; i++)
            {
                this.BitmapCache[i] = new Bitmap(map, ((int) map.Position) + (i * 0x74));
            }
            this.Map = map;
        }

        public int IndexOf(Bitmap bitmap)
        {
            List<Bitmap> list = new List<Bitmap>(this.BitmapCache);
            return list.IndexOf(bitmap);
        }

        public void Internalise(ResourceStream MainMenu, ResourceStream Shared, ResourceStream SpShared)
        {
            ResourceStream stream;
            int num3;
            this.Map.Status = "Getting Bitmap Data...";
            BitmapDataChunk[] chunkArray = new BitmapDataChunk[this.BitmapCache.Length];
            int num = this[0].LOD1Offset;
            long num2 = num & ((long) 0xc0000000L);
            num &= 0x3fffffff;
            long num8 = num2;
            if (num8 <= 0x40000000L)
            {
                if (num8 == 0L)
                {
                    stream = new ResourceStream(this.Map.Stream);
                }
                else
                {
                    if (num8 != 0x40000000L)
                    {
                        return;
                    }
                    stream = MainMenu;
                }
            }
            else
            {
                switch (num8)
                {
                    case 0x80000000L:
                        stream = Shared;
                        goto Label_008D;

                    case 0xc0000000L:
                        stream = SpShared;
                        goto Label_008D;

                    default:
                        return;
                }
            }
        Label_008D:
            num3 = 0;
            for (int i = 0; i < this.BitmapCache.Length; i++)
            {
                chunkArray[i] = new BitmapDataChunk();
                stream.Position = this[i].LOD1Offset & 0x3fffffff;
                chunkArray[i].LOD1 = stream.ReadBytes(this[i].LOD1Size);
                stream.Position = this[i].LOD2Offset & 0x3fffffff;
                chunkArray[i].LOD2 = stream.ReadBytes(this[i].LOD2Size);
                stream.Position = this[i].LOD3Offset & 0x3fffffff;
                chunkArray[i].LOD3 = stream.ReadBytes(this[i].LOD3Size);
                num3 += this.Map.CalculatePaddingSize((chunkArray[i].LOD1.Length + chunkArray[i].LOD2.Length) + chunkArray[i].LOD1.Length, 0x200);
            }
            int num5 = num3;
            foreach (BitmapDataChunk chunk in chunkArray)
            {
                num5 += (chunk.LOD1.Length + chunk.LOD2.Length) + chunk.LOD3.Length;
            }
            this.Map.Stream.SetLength(this.Map.Length + num5);
            Header header = this.Map.Header;
            header.Filesize += num5;
            this.Map.Status = "Moving Data...";
            int indexOffset = this.Map.Header.IndexOffset;
            this.Map.WriteAt(indexOffset + num5, this.Map.ReadBytesAt(indexOffset, ((int) this.Map.Length) - indexOffset, true), true);
            this.Map.Header.IndexOffset = indexOffset + num5;
            this.Map.Status = "Writing Bitmap Data...";
            this.Map.Position = indexOffset;
            for (int j = 0; j < chunkArray.Length; j++)
            {
                Bitmap bitmap1 = this[j];
                bitmap1.ChunkStartOffset += num5;
                this[j].LOD1Offset = (int) this.Map.Position;
                this.Map.Write(chunkArray[j].LOD1);
                this[j].LOD2Offset = (int) this.Map.Position;
                this.Map.Write(chunkArray[j].LOD2);
                this[j].LOD3Offset = (int) this.Map.Position;
                this.Map.Write(chunkArray[j].LOD3);
                this.Map.Write(new byte[this.Map.CalculatePaddingSize((chunkArray[j].LOD1.Length + chunkArray[j].LOD2.Length) + chunkArray[j].LOD1.Length, 0x200)]);
            }
            this.Map.Flush();
            this.Map.Reload();
            this.Map.Status = "Bitmap Internalised Successfully.";
        }

        public int Count
        {
            get
            {
                return this.BitmapCache.Length;
            }
        }

        public Bitmap this[int index]
        {
            get
            {
                return this.BitmapCache[index];
            }
            set
            {
                this.BitmapCache[index] = value;
            }
        }

        public class BitmapDataChunk
        {
            public byte[] LOD1;
            public byte[] LOD2;
            public byte[] LOD3;
        }
    }
}

